package cn.medemede.servlet;

import cn.medemede.servlet.action.Action;
import cn.medemede.servlet.factory.ActionFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "JServlet")
public class JServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/text;charset=GBK");
        String actionName=req.getParameter("actionName");
        PrintWriter out = resp.getWriter();
        System.out.println("Action="+actionName);
        if(actionName.equals("six")){
            resp.sendRedirect("six.jsp");
        }else if(actionName.equals("end")){
            resp.sendRedirect("end.jsp");
        }
        else {
            Action action;
            switch (actionName) {
                case "pattern": {
                    action = ActionFactory.getPatterns();
                    break;
                }
                case "Log_check": {
                    action = ActionFactory.getLogCheck();
                    break;
                }
                case "Do_join": {
                    action = ActionFactory.getDoJoin();
                    break;
                }
                case "Do_change": {
                    action = ActionFactory.getDoChenge();
                    break;
                }
                case "clear": {
                    action = ActionFactory.getClear();
                    break;
                }
                default:
                    throw new RuntimeException("请求失败！");
            }
            String forward = "";
            try {
                forward = action.execute(req, resp);
                System.out.println(forward);
                if (forward.equals("PatternView")) {
                    resp.sendRedirect("PatternView.jsp");
                } else {
                    out.println(forward);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
